package com.atguigu.lease.common.converter;

import com.atguigu.lease.model.enums.ItemType;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

//@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {

    @Override
    public ItemType convert(String source){
        //1.获取枚举中的所有类型
        ItemType[] values = ItemType.values();
        //2.遍历所有类型
        for (ItemType itemType : values){
           //3.当前遍历的类型的source值和传进来的值相等
            if(itemType.getCode().equals(Integer.valueOf(source))){
                return itemType;
            }
        }
        throw new IllegalArgumentException("传入的数值非法");
    }



}
